package com.zhbr.mvp.ui.fragment;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhbr.R;
import com.zhbr.adapter.OrderCardAdapter;
import com.zhbr.api.IApiService;
import com.zhbr.common.RetrofitHelper;
import com.zhbr.common.rx.BaseSubscriber;
import com.zhbr.common.rx.RxUtil;
import com.zhbr.entity.Account;
import com.zhbr.entity.MPageInfo;
import com.zhbr.entity.TGdfwPqgd;
import com.zhbr.listener.EndlessRecyclerOnScrollListener;
import com.zhbr.mvp.base.AppConstants;
import com.zhbr.mvp.base.presenter.BasePresenter;
import com.zhbr.mvp.base.view.BaseFragment;
import com.zhbr.mvp.contract.PageConstract;
import com.zhbr.mvp.presenter.PagePresenter;
import com.zhbr.mvp.ui.activity.HomeActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

import static com.zhbr.common.rx.RxUtil.applySchedulers;

public class PageFragment1 extends BaseFragment<PagePresenter> implements PageConstract.View {

    private PageViewModel1 mViewModel;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private OrderCardAdapter adapter;
    private List<TGdfwPqgd> list= new ArrayList<>();
    private MPageInfo<TGdfwPqgd> pageInfo;
    public static PageFragment1 getInstance() {
        PageFragment1 sf = new PageFragment1();
        return sf;
    }


    @Override
    protected PagePresenter createPresenter() {
        return new PagePresenter();
    }

    @Override
    public void getViewModel() {
//        mViewModel=new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(PageViewModel1.class);
    }

    @Override
    public Context getActivityContext() {
        return getContext();
    }

    @Override
    public int getContentView() {
        return R.layout.page_fragment;
    }

    @Override
    public void initView(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);
    }

    @Override
    public void initData() {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter= new OrderCardAdapter(getContext(),list);
        recyclerView.setAdapter(adapter);
        mPresenter.onGetData(0,10);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
//                showToast("滚动view");
                //在这里写获取数据的逻辑
                if (pageInfo.isHasNextPage()){
                    mPresenter.onGetData(pageInfo.getNextPage(),10);
                }else {
                    showToast("没有更多数据了！");
                }
                //获取数据后 传入adapter中上面写的更新数据的方法
//                adapter.updateData(list);
            }
        });
    }


    @Override
    public void onGetDataSuccess(MPageInfo<TGdfwPqgd> data) {
        pageInfo = data;
        list.addAll(data.getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFail(Throwable ex, String code, String msg) {
        Log.e("PageFragment1", "onGetDataFail: "+msg);
    }
}